<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$return['params']=$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['cid'])){
		$goodids=pdo_getall('jy_jdpm_goods',array('cid'=>$_RQ['cid']),array('id'),'id');
		if($goodids){
			$goodids=array_keys($goodids);
		}
		$return['params']['pid']=$_RQ['pid'];
		$return['params']['cid']=$_RQ['cid'];
		$params=array(
			'goodsid'=>$goodids ? $goodids : 0,
		);
	}else{
		if(isset($_RQ['pid'])){
			$return['params']['pid']=$_RQ['pid'];
			$pid = pdo_getcolumn('jy_jdpm_goods_group',array('name' => $_RQ['pid']),'id');
			$cid = pdo_getall('jy_jdpm_goods_group',array('pid'=>$pid),array('id'),'id');
			$cid=array_keys($cid);
			$cid[]=$pid;
			$goodids=pdo_getall('jy_jdpm_goods',array('cid'=>$cid),array('id'),'id');
			if($goodids){
				$goodids=array_keys($goodids);
			}
			$params=array(
				'goodsid'=>$goodids ? $goodids : 0,
			);
		}
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(!empty($_RQ['order_no'])){
		$params['order_no']=$_RQ['order_no'];
		$return['params']['order_no']=$_RQ['order_no'];
	}
	if(isset($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		
		$time_range=json_decode($time_range,true);
		
		$return['params']['createtime']=$time_range;
		$params['time between']=array($time_range['tstart'],$time_range['tend']);
	}
	if(!$return['params']['createtime']){
		
		//$return['params']['createtime']=array('tstart'=>time(),'tend'=>time());
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	
	$fields=array('id','pid','orderid');
	$list=pdo_getslice('jy_jdpm_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$order = get_order($item['orderid']);
			$item =array_merge($item,$order);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			if($item['getgoodstime']){
				$item['getgoodstime']=date('Y/m/d H:i:s',$item['getgoodstime']);
			}else{
				$item['getgoodstime']='还未收货';
			}
			$user=pdo_get('core_members',array('id'=>$item['mid']),array('avatar','account','nickname'));
			$seller=pdo_get('core_members',array('id'=>$item['merchid']),array('avatar','account','nickname'));
			$good=get_goodsinfo($item['pid']);
			$auction=pdo_get('jy_jdpm_auction',array('id'=>$item['pid']),array('auction_no','certificate_no'));
			$item['auction_no']=$auction['auction_no'];
			$item['certificate_no']=$auction['certificate_no'];
			$item['image']=$good['image'];
			$item['seller']=$seller['account'];
			$item['selleravatar'] = $seller['avatar']?$seller['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['sellernickname']=mb_substr($seller['nickname'],0,4,'utf-8').'***';
			$item['user']=$user['account'];
			$item['useravatar'] = $user['avatar']?$user['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['usernickname']=mb_substr($user['nickname'],0,4,'utf-8').'***';
			$item['title']=mb_substr($good['title'],0,10,'utf-8').'***';
			$item['total']=$item['price']+$item['freight'];
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单列表'),
		),
		'export'=>'export',
		'export_title'=>'导出订单数据',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('type'=>'img_string','title'=>'订单号/拍品','img'=>'image',
			'data'=>array(
				array('title'=>'订单号:','name'=>'order_no'),
				array('title'=>'拍品名称:','name'=>'title','unit'=>'sex'),
				array('title'=>'生成时间:','name'=>'createtime')
				)
			),
			array('type'=>'img_string','title'=>'拍品编号/证书编号',
			'data'=>array(
				array('title'=>'拍卖PID：','name'=>'pid'),
				array('title'=>'拍品编号：','name'=>'auction_no'),
				array('title'=>'证书编号：','name'=>'certificate_no')
				)
			),
			array('type'=>'img_string','title'=>'所属买家','img'=>'useravatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'user'),
				array('title'=>'昵称：','name'=>'usernickname')
				)
			),
			array('type'=>'img_string','title'=>'所属卖家','img'=>'selleravatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'seller'),
				array('title'=>'昵称：','name'=>'sellernickname')
				)
			),
			array('type'=>'img_string','title'=>'成交价/运费',
			'data'=>array(
				array('title'=>'成交价：','name'=>'price'),
				array('title'=>'运费：','name'=>'freight')
				)
			),
			array('type'=>'img_string','title'=>'订单总额/快递单号',
			'data'=>array(
				array('title'=>'订单总额：','name'=>'total'),
				array('title'=>'快递单号：','name'=>'express_no'),
				array('title'=>'收货时间：','name'=>'getgoodstime')
				)
			),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		),
		'action'=>array(	
			array('op'=>'send','title'=>'发货','type'=>'modal','show'=>array('status'=>1)),
			array('op'=>'address','title'=>'收货地址','type'=>'modal'),	
			array('op'=>'detail','action'=>'auction','title'=>'查看拍卖详细','type'=>'href'),			
		),
		),
		'search'=>array(
			array('type'=>'string','width'=>'8','title'=>'订单号','name'=>'order_no','help'=>''),
			array('type'=>'string','width'=>'8','title'=>'卖家UID','name'=>'merchid','help'=>''),
			array('type'=>'select','width'=>'10','title'=>'状态','name'=>'status','help'=>'','options'=>'status2'),
			//array('type'=>'group_select','width'=>'10','title'=>'分类','cid'=>'cid','pid'=>'pid','help'=>'','options'=>'cid','option_value'=>'cid','option_title'=>'pname','pid_title'=>'请选择频道','cid_title'=>'请选择子类'),
			array('type'=>'time_range','width'=>'17','title'=>'下单时间','name'=>'createtime','help'=>''),
		),
	);
	$return['pid']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0));
	$c=array();
	foreach($return['pid'] as $v){
		$n=$v['name'];
		$son=pdo_getall('jy_jdpm_goods_group',array('pid'=>$v['cid']));
		foreach($son as $s){
			$d=$s['cid'];
			$c[$n][$d]=$s['name'];
		}
	}
	$return['cid']=$c;
	$return['status2']=array(
		 array('id'=>0,'title'=>'待支付'),
		 array('id'=>1,'title'=>'已支付'),
		 array('id'=>2,'title'=>'待收货'),
		 array('id'=>3,'title'=>'待评价'),
		 array('id'=>4,'title'=>'已评价'),
		 array('id'=>5,'title'=>'申请退货'),
		 array('id'=>6,'title'=>'同意退货'),
		 array('id'=>7,'title'=>'不同意退货'),
		 array('id'=>8,'title'=>'卖家已发货'),
		 array('id'=>9,'title'=>'买家确认收货'),
		 array('id'=>10,'title'=>'已完成'),
		 array('id'=>-1,'title'=>'已过期'),
	 );
	$return['status']=array('0'=>'待支付','1'=>'已支付','2'=>'待收货','3'=>'待评价','4'=>'已评价','5'=>'申请退货','6'=>'同意退货','7'=>'不同意退货','8'=>'卖家已发货','9'=>'买家确认收货','10'=>'已完成','-1'=>'已过期');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
	}
	if(!empty($_RQ['order_no'])){
		$params['order_no']=$_RQ['order_no'];
	}
	if(isset($_RQ['status']) && $_RQ['status']!=-1){
		$params['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','pid','orderid');
	$list=pdo_getslice('jy_jdpm_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$order = get_order($item['orderid']);
			$item =array_merge($item,$order);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			if($item['getgoodstime']){
				$item['getgoodstime']=date('Y/m/d H:i:s',$item['getgoodstime']);
			}else{
				$item['getgoodstime']='还未收货';
			}
			$user=pdo_get('core_members',array('id'=>$item['mid']),array('avatar','username','nickname'));
			$seller=pdo_get('core_members',array('id'=>$item['merchid']),array('avatar','username','nickname'));
			$good=get_goodsinfo($item['pid']);
			$auction=pdo_get('jy_jdpm_auction',array('id'=>$item['pid']),array('auction_no','certificate_no'));
			$item['auction_no']=$auction['auction_no'];
			$item['certificate_no']=$auction['certificate_no'];
			$item['image']=$good['image'];
			$item['seller']=$seller['username'];
			$item['selleravatar'] = $seller['avatar']?$seller['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['sellernickname']=mb_substr($seller['nickname'],0,4,'utf-8').'***';
			$item['user']=$user['username'];
			$item['useravatar'] = $user['avatar']?$user['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['usernickname']=mb_substr($user['nickname'],0,4,'utf-8').'***';
			$item['title']=mb_substr($good['title'],0,10,'utf-8').'***';
			$item['total']=$item['price']+$item['freight'];
		}
	}
	$header=array(
		'order_no'=>'订单号',
		'title'=>'拍品名称',			
		'createtime'=>'生成时间',
		'getgoodstime'=>'买家收货时间',
		'auction_no'=>'拍品编号',
		'certificate_no'=>'证书编号',
		'user'=>'所属买家',
		'seller'=>'所属卖家',
		'price'=>'成交价',
		'freight'=>'运费',
		'total'=>'订单总额',
		'express_no'=>'快递单号',
		'status'=>'状态',
	
	);
	cfc('tools')->export2excel($header,$list,'订单列表');
}
if($_RQ['op']=='send'){
	$id=intval($_RQ['id']);
	$item['id']=$id;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'发货信息',
		'edit'=>array(
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_value'=>'name','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'express_no'),
		)
	);
	$return['express']=Array ('0' => Array ( 'id'=> 1, 'name' => '顺丰快递' ), '1' => Array ( 'id' => 2, 'name' => '申通快递' ) ,'2' => Array ( 'id' => 3,'name' => '韵达快运' ), '3' => Array ('id' => 4, 'name' => '天天快递' ), '4' => Array ( 'id' => 5, 'name' => '圆通速递' ) ,'5' => Array ( 'id' => 6,'name' => '中通速递' ) ,'6' => Array ( 'id' => 7 ,'name' => 'ems快递' ), '7' => Array ( 'id' => 8, 'name' => '汇通快运' ), '8' => Array ( 'id' => 9, 'name' => '其它快递' )); 
	
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}

if($_RQ['op']=='address'){
	$id=intval($_RQ['id']);
	$orderid=pdo_getcolumn('jy_jdpm_order',array('id'=>$id),'orderid');	
	$order = get_order($orderid);
	$address=get_address($order['addressid']);
	$address['pcd']=json_decode($address['pcd'],true);
	$item['address']=$address['pcd']['province'].$address['pcd']['city'].$address['pcd']['district'].$address['address'];
	$item['name']=$address['name'];
	$item['mobile']=$address['mobile'];
	$htmls=array(
		'type'=>'look',
		'desc'=>'收货信息',
		'look'=>array(
			array('name'=>'truename','title'=>'收货人','type'=>'string'),			
			array('name'=>'mobile','title'=>'联系方式','type'=>'string'),
			array('name'=>'address','title'=>'收货地址','type'=>'string'),
			
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$orderid=pdo_getcolumn('jy_jdpm_order',array('id'=>$id),'orderid');	
	$data=array('express'=>$post['express'],'expresssn'=>intval($post['expresssn']),'status'=>2);
	pdo_update('jy_weishop_order',$data,array('id'=>$orderid));
	exi('发货成功');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jdpm_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

if($_RQ['op']=='do'){
	$post=get_postdata();
	$item=$this->plugin_setting('order',$post);
	exi('保存成功');
}
